package com.example.basic.web.service.warn.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.web.enums.EnableTypeEnum;
import com.example.basic.web.mapper.warn.DepotWarnMapper;
import com.example.basic.web.model.warn.DepotWarn;
import com.example.basic.web.service.common.CommonService;
import com.example.basic.web.service.warn.DepotWarnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class DepotWarnServiceImpl extends BaseEntityServiceImpl<DepotWarnMapper, DepotWarn> implements DepotWarnService {

    @Resource
    private DepotWarnMapper depotWarnMapper;

    @Resource
    private CommonService commonService;

    @Resource
    private HttpServletRequest request;

    /**
     * 解除警报/取消解除  zzl
     * @param id 警报id
     * @return
     */
    @Override
    public int releaseOrCancel(String id) {
        notNull(id,"警报id不能为空");

        DepotWarn depotWarn = this.mapper.selectById(id);

        if(depotWarn == null){
            throw new ServiceException("未查询到当前数据");
        }

        //解除警报/取消解除
        if(depotWarn.getStatus().equals(EnableTypeEnum.OFF.getType())){
            depotWarn.setStatus(EnableTypeEnum.ON.getType());
            depotWarn.setReleaseTime(commonService.getDataBaseTime());
            depotWarn.setReleasePerson((String) request.getAttribute("userCode"));
        }else {
            depotWarn.setStatus(EnableTypeEnum.OFF.getType());
            depotWarn.setReleaseTime(null);
            depotWarn.setReleasePerson("");
        }

        preUpdate(depotWarn);

        return this.mapper.updateById(depotWarn);
    }

    /**
     * 报警列表
     *
     * @param pharmCode 仓库代码
     * @param zoneCode  库区代码
     * @param status
     * @return
     */
    @Override
    public IPage<DepotWarn> queryList(Page<DepotWarn> queryPage, String pharmCode, String zoneCode, String status) {
        return depotWarnMapper.queryList(queryPage,pharmCode,zoneCode,status);
    }

    /**
     * 库位安全预警数
     * @return
     */
    @Override
    public Integer homeDepotWarn() {
        return this.mapper.homeDepotWarn();
    }

}
